package web.controller;

import com.alibaba.fastjson.JSONObject;
import web.bean.User;
import web.service.Impl.UserServiceImpl;
import web.service.UserService;
import web.utils.DBUtil;
import web.utils.R;

import java.io.*;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class UserController {
    public static final String FILE_PATH="E:\\Tools\\idea\\peixun\\wuye\\src\\web\\upload";
    UserService userService=new UserServiceImpl();
    public R uploadIcon(JSONObject jsonObject){
        Integer id=jsonObject.getInteger("id");
        String base64=jsonObject.getString("base64");
        base64=base64.trim();
        //1、base64还原为图片，保存至服务器的硬盘
        byte[] bytes = Base64.getDecoder().decode(base64);
        //写文件，文件名应该叫啥？
        String fileName= UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
        System.out.println(fileName);
        fileName=fileName+".jpg";
        FileOutputStream fos=null;
        try {
            fos =new FileOutputStream(new File(FILE_PATH+File.separator+fileName));
            fos.write(bytes);
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return R.ok().setData(fileName);
    }
    public R login(JSONObject jsonObject){
        String username=jsonObject.getString("username");
        String password=jsonObject.getString("password");
        User user=userService.login(username,password);
        if(user!=null){
            return R.ok("登录成功").setData(user);
        }else{
            return R.error("登录失败");
        }
    }
    public R updateInfo(JSONObject jsonObject){
        Integer id=jsonObject.getInteger("id");
        String username=jsonObject.getString("username");
        String telephone=jsonObject.getString("telephone");
        String password=jsonObject.getString("password");
        String newPassword=jsonObject.getString("newpassword");
        String icon=jsonObject.getString("icon");
        User user = userService.findUserById(id);
        if (!password.equals("")||!"".equals(password)){
            if (password.equals(newPassword)){
                user.setPassword(newPassword);
            }else {
                return R.error("旧密码错误！");
            }
        }
        user.setUsername(username);
        user.setTelephone(telephone);
        if(icon!=null&&!"".equals(icon)){
            user.setIcon(icon);
        }
        int i = userService.updateInfo(user);
        if (i>0){
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }
    }
    public R showImg(Map<String,String[]> map){
        String fileName=map.get("fileName")[0];
        File file = new File(FILE_PATH + File.separator + fileName);
        FileInputStream fis=null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            fis = new FileInputStream(file);
            byte[] bytes = new byte[1024 * 8];
            int len=0;
            while ((len=fis.read(bytes))!=-1){
                baos.write(bytes,0,len);
            }
            byte[] imageByte = baos.toByteArray();
            String base64 = Base64.getEncoder().encodeToString(imageByte);
            return R.ok().setData(base64);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public R selectUserAll(Map<String,String[]> map){
        List<User> users = userService.selectUser();
        return R.ok().setData(users);
    }
}
